﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Lost_and_Found.Models.DB;
using System.Text.RegularExpressions;
using Lost_and_Found.Models.ObjectManager;

namespace Lost_and_Found.Controllers
{
    public class LAFController : Controller
    {
        //
        // GET: /LAF/

        public ActionResult Index()
        {
            return View();
        }

        [Authorize(Roles = "Member, Admin")]
        public ActionResult Lost()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Lost(FormCollection formCollection)
        {
            if (ModelState.IsValid)
            {
                LAFManager manager = new LAFManager();
                LAFEntities db = new LAFEntities();
                // markItem - List of Items exits
                List<string> markItem = new List<string>();
                List<Item> listItem = new List<Item>();
                Regex check = new Regex("\\da\\d");
                Regex check2 = new Regex("-");
                Regex split = new Regex("a");
                Topic topic = new Topic();
                //// Add topic
                manager.AddInfoOfLostTopic(formCollection, topic, User.Identity.Name);
                List<string> l = new List<string>();
                List<string> test = new List<string>();
                foreach (string _formData in formCollection)
                {
                    test.Add(formCollection[_formData]);
                    if ((check.IsMatch(_formData)) && (!check2.IsMatch(_formData)))
                    {
                        // get t which is the list . t[0] = idItem, t[1] = idAttribute of item.
                        var t = split.Split(_formData);
                        // if the idItem exits in markItem
                        if (markItem.Contains(t.First()))
                        {
                            int idItem = Convert.ToInt32(t.First());
                            int idAttribute = Convert.ToInt32(t.Last());
                            var item = listItem.ElementAt(markItem.IndexOf(t.First()));
                            item.Info += db.AttributeSuggest.Find(idAttribute).Name.ToString() + "@!";
                            item.Info += formCollection["hidden-" + _formData];
                            item.Info += "@@";
                        }
                        else
                        {
                            markItem.Add(t.First());
                            int idItem = Convert.ToInt32(t.First());
                            int idAttribute = Convert.ToInt32(t.Last());
                            Item item = new Item();
                            item.TopicID = topic.TopicID;

                            item.Name += db.ItemsSuggest.Find(idItem).Name.ToString();
                            item.Info += db.AttributeSuggest.Find(idAttribute).Name.ToString() + "@!";
                            item.Info += formCollection["hidden-" + _formData];
                            item.Info += "@@";
                            listItem.Add(item);
                        }
                    }
                }
                foreach (var item in listItem)
                {
                    db.Items.Add(item);
                    db.SaveChanges();
                }
                return RedirectToAction("Search", "Home", new { topicToSearch = topic.TopicID});
            }
            else
            {
                return View(formCollection);
            }
        }

        [Authorize(Roles = "Member, Admin")]
        public ActionResult Found()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Found(FormCollection formCollection)
        {
            if (ModelState.IsValid)
            {
                LAFManager manager = new LAFManager();
                LAFEntities db = new LAFEntities();
                // markItem - List of Items exits
                List<string> markItem = new List<string>();
                List<Item> listItem = new List<Item>();
                Regex check = new Regex("\\da\\d");
                Regex check2 = new Regex("-");
                Regex split = new Regex("a");
                Topic topic = new Topic();
                //// Add topic
                manager.AddInfoOfGetTopic(formCollection, topic, User.Identity.Name);
                List<string> l = new List<string>();
                foreach (string _formData in formCollection)
                {
                    //markItem.Add(formCollection[_formData]);
                    if ((check.IsMatch(_formData)) && (!check2.IsMatch(_formData)))
                    {
                        // get t which is the list . t[0] = idItem, t[1] = idAttribute of item.
                        var t = split.Split(_formData);
                        // if the idItem exits in markItem
                        if (markItem.Contains(t.First()))
                        {
                            int idItem = Convert.ToInt32(t.First());
                            int idAttribute = Convert.ToInt32(t.Last());
                            var item = listItem.ElementAt(markItem.IndexOf(t.First()));
                            item.Info += db.AttributeSuggest.Find(idAttribute).Name.ToString() + "@!";
                            item.Info += formCollection["hidden-" + _formData];
                            item.Info += "@@";
                        }
                        else
                        {
                            markItem.Add(t.First());
                            int idItem = Convert.ToInt32(t.First());
                            int idAttribute = Convert.ToInt32(t.Last());
                            Item item = new Item();
                            item.TopicID = topic.TopicID;

                            item.Name += db.ItemsSuggest.Find(idItem).Name.ToString();
                            item.Info += db.AttributeSuggest.Find(idAttribute).Name.ToString() + "@!";
                            item.Info += formCollection["hidden-" + _formData];
                            item.Info += "@@";
                            listItem.Add(item);
                        }
                    }
                }
                foreach (var item in listItem)
                {
                    db.Items.Add(item);
                    db.SaveChanges();
                }
                return RedirectToAction("Search", "Home", new { topicToSearch = topic.TopicID });
            }
            else
            {
                return View(formCollection);
            }
        }

        public ActionResult LookupTags()
        {
            LAFEntities laf = new LAFEntities();
            ListTag Tags = new ListTag();
            Tags.tags = new List<aTag>();
            List<String> LostItemName = new List<string>();

            LostItemName = laf.ItemsSuggest.Select(m => m.Name).ToList();
            foreach (string name in LostItemName)
            {
                aTag Tag = new aTag();
                Tag.tag = name;
                Tags.tags.Add(Tag);
            }

            return Json(Tags);
        }
        public ActionResult LocationTags()
        {
            LAFEntities laf = new LAFEntities();
            ListTag Tags = new ListTag();
            Tags.tags = new List<aTag>();
            List<String> Location = new List<string>();

            Location = laf.LocationSuggest.Select(m => m.Name).ToList();
            foreach (string name in Location)
            {
                aTag Tag = new aTag();
                Tag.tag = name;
                Tags.tags.Add(Tag);
            }

            return Json(Tags);
        }
        public ActionResult AttributeTag(int attID)
        {
            LAFEntities laf = new LAFEntities();
            ListTag Tags = new ListTag();
            Tags.tags = new List<aTag>();
            string value = laf.AttributeSuggest.Single(m => m.ID.Equals(attID)).ValueSuggest;
            if (value == null)
            {
                value = "";
            }
            List<string> attSuggest = new List<string>();
            string p = " {0,}, {0,}";
            Regex myRegex = new Regex(p);
            attSuggest = myRegex.Split(value).ToList();
            foreach (string name in attSuggest)
            {
                aTag Tag = new aTag();
                Tag.tag = name;
                Tags.tags.Add(Tag);
            }

            return Json(Tags);
        }
        [HttpPost]
        public ActionResult LookupItem(String name)
        {
            List<string> list = new List<string>();
            LAFEntities laf = new LAFEntities();
            try
            {
                ItemSuggest item = laf.ItemsSuggest.Single(m => m.Name.Equals(name)); list.Add(item.ID.ToString());
                var listAtt = laf.AttributesOfItems.Where(m => m.ItemSuggestID.Equals(item.ID)).ToList();
                int n = listAtt.Count; for (int i = 0; i < n; i++)
                {
                    int att = listAtt[i].AttributeSuggestID;
                    list.Add(att.ToString());
                    string attname = laf.AttributeSuggest.Single(m => m.ID.Equals(att)).Name;
                    list.Add(attname);
                }
                return Json(list);
            }
            catch
            {
                return null;
            }
        }

    }
}
